<%--
  Created by IntelliJ IDEA.
  User: Raytine
  Date: 2020/1/13
  Time: 15:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="author" content="Kodinger">
    <title>mysqlExecute 注册</title>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="css/my-login.css">
    <link href="img/favicon.png" rel="shortcut icon" />

</head>
<body class="my-login-page">
<section class="h-100">
    <div class="container h-100">
        <div class="row justify-content-md-center h-100">
            <div class="card-wrapper">
                <div class="brand">
                    <img src="img/timg.jpg">
                </div>
                <div class="card fat">
                    <div class="card-body">
                        <h4 class="card-title">注册</h4>
                        <form method="POST">

                            <div class="form-group">
                                <label for="name">用户名</label>
                                <input id="name" type="text" class="form-control" name="name" required autofocus>
                            </div>

                            <div class="form-group">
                                <label for="email">邮箱</label>
                                <input id="email" type="email" class="form-control" name="email" required>
                                <br>
                                <button id="emailVeriGet"type="button" class="btn btn-primary btn-block">
                                    获取邮箱验证码
                                </button>
                                <br>
                                <center><img  id="emailVeriGetDisImg"></center>
                                <input type="hidden" id="sendEmialFlag" value="false"/>
                                <br>
                                <label for="emailVeri">验证码</label>
                                <input id="emailVeri" type="text" class="form-control" name="name" required>

                            </div>


                            <div class="form-group">
                                <label for="password">密码</label>
                                <input id="password" type="password" class="form-control" name="password" required data-eye>
                            </div>

                            <div class="form-group">
                                <label>
                                    <input id="arge" type="checkbox" name="aggree" value="1"> 我已认真阅读，并同意mysqlExecute的注册协议。
                                </label>
                            </div>

                            <div class="form-group no-margin">
                                <button id="submit" type="button" class="btn btn-primary btn-block">
                                    注册
                                </button>
                            </div>
                            <br>
                            <center><img  id="submitDisImg"></center>
                        </form>
                    </div>
                </div>
                <div class="footer">
                    Copyright&copy;NextStep Company 2020 我们砥砺前行，不畏风雨。<br><br>
                    <a href="https://blog.csdn.net/weixin_40652498" title="下一步科技" target="_blank">下一步科技</a>
                </div>
            </div>
        </div>
    </div>
</section>

<script src="js/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="js/my-login.js"></script>
</body>

<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
<script type="text/javascript">
document.getElementById("emailVeriGet").onclick=function(){

    var email = document.getElementById("email").value;
    if(email==""){
        alert("请输入邮箱地址。");
        return ;
    }
    document.getElementById("emailVeriGetDisImg").src="img/wait.gif";
    $.ajax({
        url:"user/sendRegisterEmailVeri",     //请求地址
        contentType:"application/json;charset=UTF-8",  //传输数据类型
        data:JSON.stringify({email:email}),  //要传输的数据
        dataType:"json",   //返回值类型
        type:"post",     //请求方式
        success:function(msg){  //data表示服务端响应的数据
            if("success"==msg){
                alert("邮件发送成功，请注意查收。");
                document.getElementById("emailVeriGetDisImg").src="";
            }else {
                alert("邮件发送失败，邮箱输入错误，或者该邮箱可能已被注册过。");
                document.getElementById("emailVeriGetDisImg").src="";

            }
        }
    });

}
    document.getElementById("submit").onclick=function(){


        var name = document.getElementById("name").value;
        var email = document.getElementById("email").value;
        var emailVeri = document.getElementById("emailVeri").value;
        var password = document.getElementById("password").value;
        if(name==""){
            alert("请输入名称。");
            return ;
        }
        if(email==""){
            alert("请输入邮箱。");
            return ;
        }

        if(emailVeri==""){
            alert("请输入邮箱验证码。");
            return ;
        }
        if(password==""){
            alert("请输入密码。");
            return ;
        }
        if(!$("#arge").is(":checked")){
            alert("请勾选同意协议");
            return ;
        }
        document.getElementById("submitDisImg").src="img/wait.gif";
        var userid = null;  //注册用户的id
        $.ajax({
            url:"user/add",     //请求地址
            contentType:"application/json;charset=UTF-8",  //传输数据类型
            data:JSON.stringify({name:name,password: password,email:email,emailVeri:emailVeri}),  //要传输的数据
            dataType:"json",   //返回值类型
            type:"post",     //请求方式
            async: false,
            success:function(uid){  //data表示服务端响应的数据
                if(""==uid||null==uid){
                    alert("注册失败，请检查注册资料后重试。");
                    document.getElementById("submitDisImg").src="";
                    return ;
                }else {
                    userid=uid;
                }
            }
        });

        $.ajax({
            url:"db/newUserAddDefaultData",     //请求地址
            type:"post",     //请求方式
            async: false,
            data:{userid:userid},  //要传输的数据
            async: false,
            success:function(msg){  //data表示服务端响应的数据
                alert("注册成功！");
                window.location.href = 'login.jsp';
            }
        });

    }

</script>
</html>
